package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Dish;
import com.example.Dto.DishDto;
import com.example.domain.DishFlavor;
import com.example.mapper.DishMapper;
import com.example.service.IDishFlavorService;
import com.example.service.IDishService;
import com.example.service.ex.InsertException;
import com.example.service.ex.UpdateException;
import com.example.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 餐品业务与分类业务相关
 */
@Service
public class DishServiceImpl implements IDishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private IDishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Dish> getByCategoryId(Long id) {
        //创建条件构造器
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //传递条件
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);
        //使用MP查询数据并返回
        List<Dish> list = dishMapper.selectList(lambdaQueryWrapper);
        return list;
    }

    @Override
    public void save(DishDto dishDto) {
        //使用MP相关方法添加餐品
        int insert = dishMapper.insert(dishDto);
        if (insert!=1){
            throw  new InsertException("在添加菜品时报未知异常");
        }
        //一个餐品的口味可能有多种所以传递的数据要进行遍历，并为口味绑定餐品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor :flavors) {
            //口味添加上餐品的id
            dishFlavor.setDishId(dishDto.getId());
            //将口味添加到口味表中
            dishFlavorService.save(dishFlavor);
        }
        //定义Redis中的key
        String key = "redis_"+ dishDto.getCategoryId() + "_1";
        redisTemplate.delete(key);
    }

    @Override
    public Page<DishDto> page(Integer page, Integer pageSize, String name) {
        //创建分页对象
        Page<DishDto> dishDtoPage = new Page<DishDto>(page,pageSize);
        //调用自定义方法获取数据
        List<DishDto> list = dishMapper.list(dishDtoPage, name);
        //为分页对象设置结果集
        dishDtoPage.setRecords(list);
        return dishDtoPage;
    }
    //根据餐品id查询所有的餐品id以及餐品的味道
    @Override
    public DishDto getById(Long id) {
        DishDto dishDto = new DishDto();
        //根据id查询餐品数据
        Dish dish = dishMapper.selectById(id);
        //将查询到的餐品数据拷贝达到dishDto对象中
        BeanUtils.copyProperties(dish,dishDto);
        //获取味道数据
        List<DishFlavor> list = dishFlavorService.list(id);
        dishDto.setFlavors(list);
        return dishDto;
    }
    //修改餐品
    @Override
    public void update(DishDto dishDto) {
        //修改餐品
        int i = dishMapper.updateById(dishDto);
        if (i!=1){
            throw new UpdateException("数据修改时产生未知的异常");
        }
        //将味道删除
        dishFlavorService.delete(dishDto);
        //将味道添加,味道对象封装在DishDto中的集合中所以需要遍历添加数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavorService.save(dishFlavor);
        }
        //定义Redis中的key
        String key = "redis_"+ dishDto.getCategoryId() + "_1";
        redisTemplate.delete(key);
    }
//    //在添加套餐时根据分类id查询分类id所对应的餐品
//    @Override
//    public List<Dish> list(Dish dish) {
//        //创建条件构造器
//        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        //添加条件
//        lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime).eq(Dish::getStatus,1);
//        //调用方法查询数据
//        List<Dish> dishes = dishMapper.selectList(lambdaQueryWrapper);
//        return dishes;
//    }
    @Override
    public List<DishDto> list(Dish dish) {
        List<DishDto> list = new ArrayList<>();
        //创建一个key
        String key = "redis_"+dish.getCategoryId()+"_"+dish.getStatus();
        //查询redis中是否有数据
        List<DishDto> dtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);
        //判断redis中是否有数据
        if (dtoList!=null&&dtoList.size()>0){
            return dtoList;
        }
        //创建条件构造器
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime).eq(Dish::getStatus,1);
        //调用方法查询数据,查询的只是餐品信息，没有移动端所需要的味道信息
        List<Dish> dishes = dishMapper.selectList(lambdaQueryWrapper);
        //遍历查询出来的餐品信息
        for (Dish dish1 :dishes) {
            DishDto dishDto = new DishDto();
            //将餐品信息拷贝到dishDto中
            BeanUtils.copyProperties(dish1,dishDto);
            //根据餐品的id查询出味道的信息
            List<DishFlavor> list1 = dishFlavorService.list(dish1.getId());
            //将味道信息存储到DishDto对象
            dishDto.setFlavors(list1);
            list.add(dishDto);
        }
        redisTemplate.opsForValue().set(key,list);
        return list;
    }
}
